package sales.gz.com.salescounter.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import sales.gz.com.salescounter.R;
import sales.gz.com.salescounter.adapter.SelectListAdapter;
import sales.gz.com.salescounter.bean.PassBean;
import sales.gz.com.salescounter.bean.Passagewaybean;
import sales.gz.com.salescounter.utils.N01_Dev_YM;
import sales.gz.com.salescounter.utils.ShareUtil;
import android.widget.PopupWindow.OnDismissListener;

public class PassagewayActivity extends Activity implements SelectListAdapter.OnItemClickListener,OnDismissListener {

    @Bind(R.id.tx_title)
    TextView title;
    @Bind(R.id.recyckerview)
    RecyclerView recyckerview;
    @Bind(R.id.choice_pass)
    TextView choicePass;
    @Bind(R.id.btn_try)
    Button btnTry;
    @Bind(R.id.product_img)
    LinearLayout productImg;
    @Bind(R.id.passway)
    TextView passway;
    @Bind(R.id.activity_main)
    RelativeLayout activityMain;
    private MyAdapter myAdapter;
    private N01_Dev_YM milk_ctrl_dev;
    @Bind(R.id.ly_left)
    LinearLayout lyleft;
    private PopupWindow mSelectWindow;
    private LayoutInflater mInflater;
    private List<Passagewaybean> passlists;
    private List<PassBean> beanlist;
    private SelectListAdapter adapter;
    private ShareUtil shareUtil;
    private ListView userlist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trystoke);
        ButterKnife.bind(this);
        initview();
        savedata();
    }

    private void initview() {
        title.setText("货道测试");
        RecyclerView.LayoutManager lm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyckerview.setLayoutManager(lm);
        myAdapter = new MyAdapter();
        recyckerview.setAdapter(myAdapter);
        milk_ctrl_dev = new N01_Dev_YM("dev/ttyO2");
        passlists = new ArrayList<Passagewaybean>();
        beanlist = new ArrayList<PassBean>();
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        shareUtil = new ShareUtil(PassagewayActivity.this,"salecount");

    }
    public void onDownArrowClicked() {
        if (passlists.size() != 0) {
            showAccountChoiceWindow();
        }
    }
    private void showAccountChoiceWindow() {
        View view = mInflater.inflate(R.layout.input_selectlist, null);
        RelativeLayout contentview = (RelativeLayout) view.findViewById(R.id.input_select_listlayout);
        userlist = (ListView) view.findViewById(R.id.input_select_list);
        userlist.setDividerHeight(0);

        adapter = new SelectListAdapter(PassagewayActivity.this, passlists);
        adapter.setOnItemClickListener(this);
        userlist.setAdapter(adapter);
        mSelectWindow = new PopupWindow(contentview, choicePass.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mSelectWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.bg_input_middle_normal));
        mSelectWindow.setOutsideTouchable(true);
        mSelectWindow.setFocusable(true);
        mSelectWindow.setOnDismissListener(this);
        mSelectWindow.showAsDropDown(choicePass, 0, 0);
    }

    @OnClick({R.id.ly_left,R.id.choice_pass})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_left:
                finish();
                break;
            case R.id.choice_pass:
                onDownArrowClicked();
                break;
        }
    }

    @Override
    public void onDismiss() {

    }
    @Override
    public void onItemClicked(int position) {
        mSelectWindow.dismiss();
        choicePass.setText(passlists.get(position).getPassname());
        passway.setText(passlists.get(position).getPassname());
        beanlist = passlists.get(position).getPassBeen();
        recyckerview.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View item = LayoutInflater.from(PassagewayActivity.this).inflate(R.layout.activity_trystoke_item, parent, false);
            MyHolder myHolder = new MyHolder(item);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(MyHolder holder, final int position) {
            holder.txNumber.setText(beanlist.get(position).getPassname());
            holder.txTry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(beanlist.get(position).getPasswayid()==1)
                    {
                        List<String> data = new ArrayList<String>();
                        data.add("10");
                        data.add("11");
                        data.add("12");
                        data.add("13");
                        data.add("14");
                        data.add("15");
                        data.add("16");
                        data.add("17");
                        data.add("18");
                        data.add("19");

                        milk_ctrl_dev.PutStream(data);
                    }else {
                        milk_ctrl_dev.Start(beanlist.get(position).getPasswayid(), beanlist.get(position).getId());
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return beanlist.size();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tx_number)
        TextView txNumber;
        @Bind(R.id.tx_try)
        TextView txTry;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void savedata() {
        if (0 == (ShareUtil.getDataList("passdata").size())) {
            List<Passagewaybean> passlist = new ArrayList<Passagewaybean>();
            for (int index = 1; index <= 6; index++) {
                if (index == 1) {
                    Passagewaybean pass = new Passagewaybean();
                    List<PassBean> lists = new ArrayList<PassBean>();
                    pass.setPassname("第" + index + "层");
                    pass.setId(index);
                    for (int i = 10; i <= 19; i++) {
                        PassBean beans = new PassBean();
                        beans.setId(i);
                        beans.setPassname(i + "通道");
                        beans.setPasswayid(index);
                        lists.add(beans);
                    }
                    pass.setPassBeen(lists);
                    passlist.add(pass);

                } else if (index == 2) {
                    Passagewaybean pass = new Passagewaybean();
                    List<PassBean> lists = new ArrayList<PassBean>();
                    pass.setPassname("第" + index + "层");
                    pass.setId(index);
                    for (int i = 20; i <= 29; i++) {
                        PassBean beans = new PassBean();
                        beans.setId(i);
                        beans.setPassname(i + "通道");
                        beans.setPasswayid(index);
                        lists.add(beans);

                    }
                    pass.setPassBeen(lists);
                    passlist.add(pass);
                }
                if (index == 3) {
                    Passagewaybean pass = new Passagewaybean();
                    List<PassBean> lists = new ArrayList<PassBean>();
                    pass.setPassname("第" + index + "层");
                    pass.setId(index);
                    for (int i = 30; i <= 39; i++) {
                        PassBean beans = new PassBean();
                        beans.setId(i);
                        beans.setPassname(i + "通道");
                        beans.setPasswayid(index);
                        lists.add(beans);
                    }
                    pass.setPassBeen(lists);
                    passlist.add(pass);
                }
                if (index == 4) {
                    Passagewaybean pass = new Passagewaybean();
                    List<PassBean> lists = new ArrayList<PassBean>();
                    pass.setPassname("第" + index + "层");
                    pass.setId(index);
                    for (int i = 40; i <= 49; i++) {
                        PassBean beans = new PassBean();
                        beans.setId(i);
                        beans.setPassname(i + "通道");
                        beans.setPasswayid(index);
                        lists.add(beans);
                    }
                    pass.setPassBeen(lists);
                    passlist.add(pass);
                }
                if (index == 5) {
                    Passagewaybean pass = new Passagewaybean();
                    List<PassBean> lists = new ArrayList<PassBean>();
                    pass.setPassname("第" + index + "层");
                    pass.setId(index);
                    for (int i = 50; i <= 59; i++) {
                        PassBean beans = new PassBean();
                        beans.setId(i);
                        beans.setPassname(i + "通道");
                        beans.setPasswayid(index);
                        lists.add(beans);
                    }
                    pass.setPassBeen(lists);
                    passlist.add(pass);
                }
                if (index == 6) {
                    Passagewaybean pass = new Passagewaybean();
                    List<PassBean> lists = new ArrayList<PassBean>();
                    pass.setPassname("第" + index + "层");
                    pass.setId(index);
                    for (int i = 60; i <= 69; i++) {
                        PassBean beans = new PassBean();
                        beans.setId(i);
                        beans.setPassname(i + "通道");
                        beans.setPasswayid(index);
                        lists.add(beans);
                    }
                    pass.setPassBeen(lists);
                    passlist.add(pass);
                }

            }

            ShareUtil.setDataList("passdata", passlist);
            passlists.addAll(ShareUtil.<Passagewaybean>getDataList("passdata"));
        }else {
            passlists.addAll(ShareUtil.<Passagewaybean>getDataList("passdata"));
        }
        choicePass.setText(passlists.get(0).getPassname());
        beanlist.clear();
        beanlist = passlists.get(0).getPassBeen();
    }

}
